/**
 * Created by Administrator on 2017/3/21.
 * 数据库操作
 */
const mongoose = require('mongoose'),
    winston = require('winston'),
    rootConfig=require('../config/rootConfig'),
    db=mongoose.connect(rootConfig.mongooseDbUrl);//链接数据库




/**
 * @function 增加记录
 * @author qiwen
 * @date 2018-08-08
 * @param
 */
exports.add = (model,param, callback) => {
    model.save(param).then(record => {
        callback(null, record);
    }).catch(err => {
        callback(err, null);
    });
};


/**
 * @function 删除记录
 * @author yelin
 * @date 2018-08-08
 * @param
 */
exports.del=(model,param, callback) => {
    model.destroy({where:param}).then(record => {
        callback(null, record);
    }).catch(err => {
        callback(err, null);
    });
}

/**
 * @function 分页查询
 * @author Eric
 * @date 2018-05-28
 * @param
 */
exports.findByPage = (model,param, callback) => {
    let obj={}
    model.findAndCountAll(param).then(record => {
        obj.return='0'
        obj.code='0'
        obj.message='查询成功！'
        obj.rowcount=record.count
        obj.data=record.rows
        callback(null, obj);
    }).catch(err => {
        obj.return='-1'
        obj.code='-1'
        obj.message='查询失败'
        obj.data=err
        callback(obj, null);
    });
}

/**
 * @function 查询所有记录
 * @author Eric
 * @date 2018-05-28
 * @param
 */
exports.find = (model,param, callback) => {
    model.findAll(param).then(record => {
        callback(null, record);
    }).catch(err => {
        callback(err, null);
    });
}
/**
 * @function 查询单条记录
 * @author Eric
 * @date 2018-05-28
 * @param
 */
exports.find = (model,param, callback) => {
    model.findOne(param).then(record => {
        callback(null, record);
    }).catch(err => {
        callback(err, null);
    });
}
/**
 * @function 修改记录
 * @author yelin
 * @date 2017=8-08-08
 * @param
 */
exports.update = (model,updateparams,criteria, callback) => {
    gps_role.update(updateparams,criteria).then(record => {
        callback(null, record);
    }).catch(err => {
        callback(err, null);
    });
}

